package com.jiuyuan.controller;


import com.jiuyuan.entity.Admin;
import com.jiuyuan.entity.vo.RespBean;
import com.jiuyuan.service.IAdminRoleService;
import com.jiuyuan.service.IAdminService;
import com.jiuyuan.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiuyuan
 * @since 2023-02-15
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Autowired
    IAdminService adminService;
    @Autowired
    IRoleService roleService;
    @Autowired
    IAdminRoleService adminRoleService;

    @ApiOperation("获取所有操作员")
    @GetMapping("/")
    /**
     * @Description: 关键字，id从环境中获取
     * @Param:  * @param keyBoard
     * @return: com.jiuyuan.entity.vo.RespBean
     * @Author: Feng
     * @Date:2023/02/27 10:47
     */
    public RespBean list(String keyBoard){
        return adminService.allList(keyBoard);
    }

    @ApiOperation("更新操作员")
    @PutMapping("/")
    public RespBean update(@RequestBody Admin admin){
        boolean update = adminService.updateById(admin);
        if(update) return RespBean.success("更新成功");
        return RespBean.error("更新失败");
    }

    @ApiOperation("删除操作员")
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable Integer id){
        boolean remove = adminService.removeById(id);
        if(remove) return RespBean.success("删除成功");
        return RespBean.error("删除失败");
    }

    @ApiOperation("获取所有角色")
    @GetMapping("/allRoles")
    public RespBean roleLists(){
        return RespBean.success(roleService.list());
    }

    @ApiOperation("更新操作员的角色")
    @PutMapping("/{adminId}")
    /**
     * @Description: 将用户对应的角色全部删掉，再添加上
     * @Param:  * @param adminId
     * @param roleIds
     * @return: com.jiuyuan.entity.vo.RespBean
     * @Author: Feng
     * @Date:2023/02/27 10:43
     */
    public RespBean updateRoles(@PathVariable Integer adminId,Integer[] roleIds){
        return adminRoleService.updateRoles(adminId,roleIds);
    }

}
